package cn.lyjuan.payment.third.base.model.chl.domain;

import cn.lyjuan.base.exception.util.ErrUtils;
import cn.lyjuan.payment.third.base.common.entity.app.AppChlEntity;
import cn.lyjuan.payment.third.base.common.enums.impl.AppChlStatusEnum;
import cn.lyjuan.payment.third.base.common.enums.impl.ChlEnum;
import cn.lyjuan.payment.third.base.model.exception.ErrCodeImpl;
import cn.lyjuan.payment.third.base.common.mapper.AppChlEntityMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import tk.mybatis.mapper.entity.Example;

import java.time.LocalDateTime;
import java.util.List;

@Component
@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
public class AppChlModel
{
    @Autowired
    private AppChlEntityMapper mapper;

    private AppChlEntity entity;

    /**
     * 分发为实际的对象
     *
     * @return
     */
    public IChlCnfModel dispatch()
    {
        IChlCnfModel cnfModel = ChlCnfScanner.dispatch(this.getEntity().getChlType());

        // 理论上应该存在
        boolean exist = cnfModel.query(this.entity.getAppId());

        return cnfModel;
    }

    /**
     * 新增
     *
     * @param appId   新增的AppID
     * @param chlType 新增的支付渠道类型
     * @return
     */
    public boolean add(Integer appId, ChlEnum chlType)
    {
        // 判断已存在
        if (exist(appId, chlType))
            ErrUtils.appThrow(ErrCodeImpl.APP_CHL_EXIST);

        this.entity = new AppChlEntity();
        this.entity.setAppId(appId);
        this.entity.setChlType(chlType);

        this.entity.setStatus(AppChlStatusEnum.CONFIMING);
        this.entity.setCreatetime(LocalDateTime.now());

        this.mapper.insert(this.entity);

        return true;
    }

    /**
     * 判断应用的支付渠道配置信息是否已存在
     *
     * @param appId
     * @param chlType
     * @return
     */
    private boolean exist(Integer appId, ChlEnum chlType)
    {
        Example ex = new Example(AppChlEntity.class);
        ex.createCriteria()
                .andEqualTo("chlType", chlType)
                .andEqualTo("mchId", appId);

        return this.mapper.selectCountByExample(ex) > 0;
    }

    /**
     * 查询
     *
     * @param appId
     * @param chlType
     * @return
     */
    public boolean query(Integer appId, ChlEnum chlType)
    {
        Example ex = new Example(AppChlEntity.class);
        ex.createCriteria()
//                .andEqualTo("status", AppChlStatusEnum.NORMAL)
                .andEqualTo("chlType", chlType)
                .andEqualTo("appId", appId);

        this.entity = mapper.selectOneByExample(ex);

        return null != this.entity;
    }

    /**
     * 获取支付渠道的配置信息
     *
     * @param <T> todo 指定类型
     * @return
     */
    public <T> T chlConfig(Class<T> cls)
    {
        return null;
    }

    public AppChlEntityMapper getMapper()
    {
        return mapper;
    }

    public void setMapper(AppChlEntityMapper mapper)
    {
        this.mapper = mapper;
    }

    public AppChlEntity getEntity()
    {
        return entity;
    }

    public void setEntity(AppChlEntity entity)
    {
        this.entity = entity;
    }
}
